package com.sgb.kjwl.view.ui.activity.home;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySearchBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.SearchDeviceOutEntity;
import com.sgb.kjwl.model.entity.SearchMachineInEntity;
import com.sgb.kjwl.model.entity.SearchMachineOutEntity;
import com.sgb.kjwl.model.entity.SearchMaterialEntity;
import com.sgb.kjwl.view.ui.activity.device.DeviceDetailActivity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MachineInActivity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MachineOutActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.adapter.home.SearchAdapter;
import com.sgb.kjwl.view.ui.adapter.search.SearchDeviceOutAdapter;
import com.sgb.kjwl.view.ui.adapter.search.SearchMachineInAdapter;
import com.sgb.kjwl.view.ui.adapter.search.SearchMachineOutAdapter;
import com.sgb.kjwl.view.ui.adapter.search.SearchMaterialAdapter;
import com.sgb.kjwl.viewmodel.home.SearchModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

import javax.inject.Inject;

public class SearchActivity extends AppActivity {
    public static final int SEARCH_COUNT = 10;
    private static final String TYPE = "type";
    private ActivitySearchBinding binding;
    private SearchAdapter searchAdapter;
    private SearchMaterialAdapter materialAdapter;
    private SearchMachineOutAdapter machineOutAdapter;
    private SearchMachineInAdapter machineInAdapter;
    private SearchDeviceOutAdapter deviceOutAdapter;
    private int index = 0;
    private boolean machineOut = true;
    @Inject
    SearchModel viewModel;

    public static void start(Context context, int index) {
        Intent intent = new Intent();
        intent.putExtra(TYPE, index);
        intent.setClass(context, SearchActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(SearchActivity.this, R.layout.activity_search);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
        binding.searchView.setOnClickListener(this);
        binding.outView.setOnClickListener(this);
        binding.inView.setOnClickListener(this);

        if (searchAdapter == null) {
            searchAdapter = new SearchAdapter(SearchActivity.this, this);
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
        binding.recyclerView.setAdapter(searchAdapter);

        if (materialAdapter == null) {
            materialAdapter = new SearchMaterialAdapter(getActivity(), null);
        }
        materialAdapter.setOnItemClick(new AdapterOnItemClick<SearchMaterialEntity>() {
            @Override
            public void onItemClick(SearchMaterialEntity searchMaterialEntity, int position) {
                ShopGoodsDetailActivity.start(getActivity(), searchMaterialEntity.getId(), "", searchMaterialEntity.getSkuid());
//                String link = AppConstant.SEARCH_MATERIAL_LIST + searchMaterialEntity.getId();
//                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW).setMessageData(link));
//                WebActivity.start(getActivity());
            }
        });
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        binding.searchRecyclerView.setLayoutManager(manager);
        binding.searchRecyclerView.setLoadingListener(new com.swgk.core.xrecyclerview.XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
            }
        });
        binding.editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (viewModel.searchValue.get().equals("")) {
                    binding.searchLayout.setVisibility(View.GONE);
                    binding.emptyView.setVisibility(View.GONE);
                    binding.recyclerView.setVisibility(View.VISIBLE);
                }
            }
        });
        initMachineOutView();
        initMachineInView();
        initDeviceOutView();
    }

    private void initMachineOutView() {
        if (machineOutAdapter == null) {
            machineOutAdapter = new SearchMachineOutAdapter(SearchActivity.this, null);
        }
        machineOutAdapter.setOnItemClick(new AdapterOnItemClick<SearchMachineOutEntity>() {
            @Override
            public void onItemClick(SearchMachineOutEntity searchMachineOutEntity, int position) {
                MachineOutActivity.start(getActivity(), searchMachineOutEntity.getId());
//                String link = AppConstant.SEARCH_MACHINE_OUT_LIST + searchMachineOutEntity.getId();
//                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW).setMessageData(link));
//                WebActivity.start(getActivity());
            }
        });
    }

    private void initMachineInView() {
        if (machineInAdapter == null) {
            machineInAdapter = new SearchMachineInAdapter(SearchActivity.this, null);
        }
        machineInAdapter.setOnItemClick(new AdapterOnItemClick<SearchMachineInEntity>() {
            @Override
            public void onItemClick(SearchMachineInEntity searchMachineInEntity, int position) {
                MachineInActivity.start(getActivity(), searchMachineInEntity.getId());
//                String link = AppConstant.SEARCH_MACHINE_IN_LIST + searchMachineInEntity.getId();
//                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW).setMessageData(link));
//                WebActivity.start(getActivity());
            }
        });
    }

    private void initDeviceOutView() {
        if (deviceOutAdapter == null) {
            deviceOutAdapter = new SearchDeviceOutAdapter(SearchActivity.this, null);
        }
        deviceOutAdapter.setOnItemClick(new AdapterOnItemClick<SearchDeviceOutEntity>() {
            @Override
            public void onItemClick(SearchDeviceOutEntity searchDeviceOutEntity, int position) {
                DeviceDetailActivity.start(getActivity(), searchDeviceOutEntity.getId());

//                String link = AppConstant.SEARCH_DEVICE_OUT_LIST + searchDeviceOutEntity.getId();
//                EventBus.getDefault().postSticky(WebEvent.make().setMessageTag(EventConstant.EVENT_WEBVIEW).setMessageData(link));
//                WebActivity.start(getActivity());
            }
        });
    }

    @Override
    protected void init() {
        index = getIntent().getIntExtra(TYPE, 0);
        viewModel.queryHistory(index);
    }

    public void showSearchHot(List<String> datas) {
        searchAdapter.setSearchHot(datas);
    }

    public void showSearchHistory(List<String> datas) {
        searchAdapter.setSearchHistory(datas);
    }

    public void chooseSearchHistory(boolean choose) {
        searchAdapter.setChooseSearchHistory(choose);
    }

    public void delSearchHistory(String name) {
        viewModel.delHistory(name, index);
    }

    public void delAllSearchHistory() {
        viewModel.delAllHistory(index);
    }

    public void search(String name) {
        closeKeybord();
        binding.editSearch.setText(name);
        viewModel.saveHistory(viewModel.searchValue.get(), index);
        binding.recyclerView.setVisibility(View.GONE);
        binding.searchLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.search_view:
                if (viewModel.searchValue.get().equals("")) {
                    return;
                }
                search(viewModel.searchValue.get());
                break;
            case R.id.out_view:
                machineOut = true;
                binding.inView.setSelected(false);
                binding.outView.setSelected(true);
                binding.searchRecyclerView.setAdapter(machineOutAdapter);
                break;
            case R.id.in_view:
                machineOut = false;
                binding.outView.setSelected(false);
                binding.inView.setSelected(true);
                binding.searchRecyclerView.setAdapter(machineInAdapter);
                break;
        }
    }

    public void refreshDeviceOut(List<SearchDeviceOutEntity> datas) {
        binding.emptyView.setVisibility(View.GONE);
        binding.searchRecyclerView.setVisibility(View.VISIBLE);
        binding.searchRecyclerView.setLoadingMoreEnabled(true);
        deviceOutAdapter.setDatas(datas);
        binding.searchRecyclerView.refreshComplete();
        if (deviceOutAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreDeviceOut(List<SearchDeviceOutEntity> datas) {
        deviceOutAdapter.addDatas(datas);
        binding.searchRecyclerView.loadMoreComplete();
        if (deviceOutAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void refreshMachineIn(List<SearchMachineInEntity> datas) {
        binding.emptyView.setVisibility(View.GONE);
        binding.searchRecyclerView.setVisibility(View.VISIBLE);
        binding.searchRecyclerView.setLoadingMoreEnabled(true);
        machineInAdapter.setDatas(datas);
        binding.searchRecyclerView.refreshComplete();
        if (machineInAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreMachineIn(List<SearchMachineInEntity> datas) {
        machineInAdapter.addDatas(datas);
        binding.searchRecyclerView.loadMoreComplete();
        if (machineInAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void refreshMachineOut(List<SearchMachineOutEntity> datas) {
        binding.emptyView.setVisibility(View.GONE);
        binding.searchRecyclerView.setVisibility(View.VISIBLE);
        binding.searchRecyclerView.setLoadingMoreEnabled(true);
        machineOutAdapter.setDatas(datas);
        binding.searchRecyclerView.refreshComplete();
        if (machineOutAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreMachineOut(List<SearchMachineOutEntity> datas) {
        machineOutAdapter.addDatas(datas);
        binding.searchRecyclerView.loadMoreComplete();
        if (machineOutAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void refresh(List<SearchMaterialEntity> datas) {
        binding.emptyView.setVisibility(View.GONE);
        binding.searchRecyclerView.setVisibility(View.VISIBLE);
        binding.searchRecyclerView.setLoadingMoreEnabled(true);
        materialAdapter.setDatas(datas);
        binding.searchRecyclerView.refreshComplete();
        if (materialAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.searchRecyclerView.refreshComplete();
        binding.searchRecyclerView.setVisibility(View.GONE);
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<SearchMaterialEntity> datas) {
        materialAdapter.addDatas(datas);
        binding.searchRecyclerView.loadMoreComplete();
        if (materialAdapter.getItemCount() >= viewModel.total) {
            binding.searchRecyclerView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreComplete() {
        binding.searchRecyclerView.loadMoreComplete();
    }
}
